from HTMLParser import HTMLParser

DEBUG = 0
TAGS = ["href"]

class MyHTMLParser(HTMLParser):
   def handle_starttag(self, tag, attrs):
      if DEBUG:
         print "handle_starttag"
         print "tag:", tag
         print "attrs:", attrs
         raw_input("  ")

      if self.stack[0] == 1:
         for ts in attrs:
            for tag in TAGS:
               if ts[0] == tag and len(ts) > 1:
                  if "html" in ts[1]:
                     self.stack.append(ts[1])
                  
      if "p" in tag:
         for i in attrs:
            if "data-pid" in i:
               # turn on capture
               self.stack[0] = 1 
               self.stack.append("------BEGIN------")

   # this is never called for some reason, not sure why
   def handle_end(self, tag):
      if DEBUG:
         print "handle_end"
         print "tag:", tag
         raw_input(" ")
      if len(self.stack) > 0:
         if self.stack[0] == 1:
            if "p" in tag:
               raw_input("stop")
               # turn off capture
               self.stack[0] = 0 
               self.stack.append("------END------")
      
   def handle_data(self, data):
      if DEBUG:
         print "handle_data"
         print "data:", data
         raw_input(" ")
      if data != " ":
         if len(self.stack) > 0:
            if self.stack[0] == 1:
               self.stack.append(data)
      
   def get_row(self, line):
      self.stack = []
      self.stack.append(0)   # start in off mode
      self.feed(line)
      if self.stack:
         for i in self.stack:
            if i != 0:
               print i
    
  
def test():
   p = MyHTMLParser()
#   f = open("msg.html", "r")
   f = open("test.html", "r")
   line = f.readline()
   while (len(line) !=0):
      p.get_row(line)
#      print "\n--new readline--\n"
      line = f.readline()
   f.close()

if __name__ == "__main__":
   test()
